import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../entity/home/video_list_entity.dart';
import '../../view_model/home/recommend_video_list_view_model.dart';
import '../widget/custom_refresh.dart';
import 'VideoView.dart';

class VideoListPage extends StatelessWidget {
  final PageController pageController;

  VideoListPage({Key? key, required this.pageController}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
        child: ChangeNotifierProvider(
          create: (BuildContext context) => RecommendVideoListViewModel(),
          child: Consumer<RecommendVideoListViewModel>(
            builder: (context, viewModel, child) {
              return CustomRefresh(
                controller: viewModel.refreshController,
                enablePullUp: true,
                onRefresh: () async {
                  viewModel.onRefresh();
                },
                onLoading: () async {
                  viewModel.onLoading();
                },
                child: viewModel.datas.length > 0
                    ? PageView.builder(
                  onPageChanged: (index) {
                    if (index == viewModel.datas.length - 1) {
                      viewModel.onLoading();
                    }
                  },
                  controller: pageController,
                  scrollDirection: Axis.vertical,
                  itemCount: viewModel.datas.length,
                  itemBuilder: (BuildContext context, int index) {
                    VideoEntity videoEntity = viewModel.datas[index];
                    return Stack(
                      children: [
                        ConstrainedBox(
                          constraints: BoxConstraints.expand(width: MediaQuery.of(context).size.width),
                          child: Image.network(
                            videoEntity.imgUrl!,
                            fit: BoxFit.cover,
                          ),
                        ),
                        BackdropFilter(
                          filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
                          child: Container(
                            color: Theme.of(context).primaryColor.withOpacity(0),
                          ),
                        ),
                        VideoView(url: videoEntity.url as String, cover: videoEntity.imgUrl!),
                        Positioned(
                          left: 20,
                          bottom: 30,
                          child: _buildPageBottomContent(viewModel, index, Theme.of(context).brightness == Brightness.light),
                        ),
                      ],
                    );
                  },
                )
                    : Center(),
              );
            },
          ),
        ),
      );
  }

  Widget _buildPageBottomContent(RecommendVideoListViewModel viewModel, int index, bool isLight) {
    VideoEntity entity = viewModel.datas[index];
    return Container(
      width: 253,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "@${entity.nickName}",
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: Colors.grey,
            ),
          ),
          SizedBox(height: 2),
          Text(
            "${entity.title}" ?? "",
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
              color: Colors.grey,
            ),
            maxLines: 3,
            overflow: TextOverflow.ellipsis,
            softWrap: true,
          ),
        ],
      ),
    );
  }
}
